package com.democaffien.demos.controller;

import com.democaffien.demos.service.UserService;
import com.democaffien.demos.entity.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author rise
 * @date 2024年10月30日 15:34
 * @description 使用注解
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    @PostMapping("add")
    @CachePut(value = "user",key = "#user.id")//#p0.id、#a0.id和#root.args[0].id都是访问方法参数
    public User add(@RequestBody User user){
        //这里是将插入数据的结果写入到缓存中，
       return userService.add(user);
    }

    @GetMapping("/{id}")
    @Cacheable(value = "user",key ="#id")
    public User get(@PathVariable Integer id){
        return userService.getById(id);
    }

    @DeleteMapping("/{id}")
    @CacheEvict(value = "user",key = "#id")
    public Integer delete(@PathVariable Integer id){
        return userService.delete(id);
    }

    @DeleteMapping("all")
    @CacheEvict(value = "user",allEntries = true)//多条删除，删除缓存中user下面所有的数据
    public Integer deleteAll(){
       return userService.deleteAll();
    }


}
